package com.usb.serial.device.settingIp;


import android.os.Bundle;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.usb.serial.device.R;


import java.util.regex.Pattern;

public class SettingIpActivity extends AppCompatActivity {
    private static final Pattern IPV4_PATTERN = Pattern.compile(
            "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" +
                    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    private EditText ip1, ip2, ip3, ip4, ip5;
    private Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_ip);

        Url.context = this;

        initView();
        
        setOnClickListener();
    }

    private void setOnClickListener() {
        
        submit.setOnClickListener(v -> {
            
            if ("".equals(ip1.getText().toString().trim()) || "".equals(ip2.getText().toString().trim()) || "".equals(ip3.getText().toString().trim())
                    || "".equals(ip4.getText().toString().trim()) || "".equals(ip5.getText().toString().trim())) {
                Toast.makeText(this, "请填完整IP信息", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!isValidIPAddress(ip1.getText().toString().trim())) {
                Toast.makeText(this, "IP-1不是一个正确的IP地址", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!isValidIPAddress(ip2.getText().toString().trim())) {
                Toast.makeText(this, "IP-2不是一个正确的IP地址", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!isValidIPAddress(ip3.getText().toString().trim())) {
                Toast.makeText(this, "IP-3不是一个正确的IP地址", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!isValidIPAddress(ip4.getText().toString().trim())) {
                Toast.makeText(this, "IP-4不是一个正确的IP地址", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!isValidIPAddress(ip5.getText().toString().trim())) {
                Toast.makeText(this, "IP-5不是一个正确的IP地址", Toast.LENGTH_SHORT).show();
                return;
            }

            Url.setIp(ip1.getText().toString().trim(), 0);
            Url.setIp(ip2.getText().toString().trim(), 1);
            Url.setIp(ip3.getText().toString().trim(), 2);
            Url.setIp(ip4.getText().toString().trim(), 3);
            Url.setIp(ip5.getText().toString().trim(), 4);

            Toast.makeText(this, "所有IP设置成功", Toast.LENGTH_SHORT).show();
      ;
            finish();


        });
        
    }

    private void initView() {

        ip1 = findViewById(R.id.edt_ip1);
        ip2 = findViewById(R.id.edt_ip2);
        ip3 = findViewById(R.id.edt_ip3);
        ip4 = findViewById(R.id.edt_ip4);
        ip5 = findViewById(R.id.edt_ip5);
        submit = findViewById(R.id.submit);

        ip1.setText(Url.getIP()[0].equals("0") ? "" : Url.getIP()[0]);
        ip2.setText(Url.getIP()[1].equals("0") ? "" : Url.getIP()[1]);
        ip3.setText(Url.getIP()[2].equals("0") ? "" : Url.getIP()[2]);
        ip4.setText(Url.getIP()[3].equals("0") ? "" : Url.getIP()[3]);
        ip5.setText(Url.getIP()[4].equals("0") ? "" : Url.getIP()[4]);

    }



    public static boolean isValidIPv4(String ip) {
        return IPV4_PATTERN.matcher(ip).matches();
    }


    public static boolean isValidIPAddress(String ip) {
        return isValidIPv4(ip);
    }
}

